package com.cpit.qualitycontrol.dao;

import com.cpit.qualitycontrol.entity.ProcessRule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author Hull
 * @date 2017/9/30
 */
public interface ProcessRuleRepository extends JpaRepository<ProcessRule, Long>, JpaSpecificationExecutor<ProcessRule> {
    ProcessRule findByName(String name);
    List<ProcessRule> findByProjectId(Long projectId);
    long countProcessRulesByDeviceTypeId(Long deviceTypeId);
    long countProcessRulesByWorkOrderTypeId(Long workOrderTypeId);
    long countProcessRulesByProjectId(Long projectId);

    @Override
    @Query("SELECT new com.cpit.qualitycontrol.entity.ProcessRule(pr.id, pr.name, pr.description, pr.deviceTypeId, dt.name, " +
            "pr.workOrderTypeId, wot.name, pr.photoCount, pr.projectId, pj.name, pr.additionalBusinessId)" +
            " from ProcessRule pr, DeviceType dt, WorkOrderType wot, Project pj " +
            " where pr.deviceTypeId=dt.id and pr.workOrderTypeId=wot.id and pr.projectId=pj.id")
    List<ProcessRule> findAll();
//    @Query("SELECT new com.cpit.qualitycontrol.entity.ProcessRule(pr.id, pr.name, pr.description, pr.deviceTypeId, dt.name, " +
//            "pr.workOrderTypeId, wot.name, pr.photoCount, pr.projectId, pj.name, pr.additionalBusinessId)" +
//            " from ProcessRule pr, DeviceType dt, WorkOrderType wot, Project pj " +
//            " where pr.deviceTypeId=dt.id and pr.workOrderTypeId=wot.id and pr.projectId=pj.id")
//    List<ProcessRule> findAll(Specification<ProcessRule> specification);
}
